<template>
  <div>
    <el-table
      v-if="noResponse"
      v-loading="loading"
      style="width: 820px;height: 430px;margin-bottom: 10px;margin-top: 10px;z-index: 1">
    </el-table>
    <div v-if="isEmpty"  style="width: 820px">
      <el-empty :image-size="200"></el-empty>
    </div>
    <div v-else id="target">
      <div class="box">
        <div class="cards" v-for="(item,index) in contentData" :key="index">
          <div v-if="index==0">
            <div class="topping"></div>
            <div class="top-text">{{$t('home.blink.card.setTop')}}</div>
          </div>
          <!--      <div class="spot"></div>-->
          <div class="cards-top">
            {{item.context}}
          </div>
          <div class="cards-bottom">
            <router-link target="_blank" :to="{path:'/userHomePage',query:{userId:item.userId}}">
              <div class="cards-bottom-headPho">
                <div><img :src=item.headPhotoPath width="20px" style="border-radius: 50%;"></div>
                <div>{{item.authorNickname==""?item.userId:item.authorNickname}}</div>
              </div>
            </router-link>
            <div class="cards-bottom-pubTime">
              <i class="iconfont icon-shijian cards-bottom-icn" style="color: rgba(108,106,109,0.8)"></i>
            {{item.publishTime}}
            </div>
            <div class="cards-bottom-like">
              <a href="javascript:;" @click="onclickLike(item.blinkId,item.blinkLike+1)"><i class="iconfont icon-xihuan"></i></a>
            {{item.blinkLike}}
            </div>
          </div>
        </div>
      </div>
      <el-pagination
        v-if="!isEmpty && !noResponse"
        background
        layout="prev, pager, next"
        :total=pageTotal
        @current-change="handleCurrentChange(currentPage)"
        :current-page.sync = currentPage
        class="page">
      </el-pagination>
    </div>
  </div>
</template>

<script>

    export default {
        name: "BlinkCards",
        data(){
            return{
                loading:"true",
                isEmpty:false,
                noResponse: true,
                baseURL:'http://localhost:8081/homePage/blinkCard/',
                baseURL2:'http://localhost:8081/homePage/getPages/blinkCard',
                baseURL3:'http://localhost:8081/blink/addLike/',
                contentData:'',
                pageTotal:0,
                currentPage:1  // 当前页
            }
        },
        mounted(){
            this.getBlinkInfo()
            this.getPages()
        },
        methods:{
            getBlinkInfo(){
                this.axios.post(this.baseURL+this.currentPage).then(res=>{
                    this.noResponse=false
                    if (res.data == false){
                        this.$message.error(this.$t('home.blink.card.serviceUnusual'))
                    } else if (res.data == "empty") {
                        this.isEmpty = true
                    } else {
                        this.contentData = res.data
                    }
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            getPages(){
                this.axios.post(this.baseURL2).then(res=>{
                    this.pageTotal = res.data * 10
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            onclickLike(blinkId,likeCount){
                this.axios.post(this.baseURL3 + blinkId + "/" + likeCount).then(res=>{
                    if (res.data == true) {
                        this.$message.success(this.$t('home.blink.card.like'))
                        this.getBlinkInfo()
                    } else {
                        this.$message.error(this.$t('home.blink.card.serviceUnusual'))
                    }
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            handleCurrentChange(currentPage){
                this.axios.post(this.baseURL+currentPage).then(res=>{
                    this.noResponse=false
                    if (res.data != "empty"){
                        this.contentData = res.data
                        // this.getHeadPhoto(res.data)
                        let target = document.getElementById("target").offsetTop
                        window.scrollTo({
                            top:target,
                            behavior: "smooth"
                        });
                    }
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
        }
    }
</script>

<style scoped>
  .box{
    width: 820px;
    min-height: 115px;
    border-left: rgb(206, 206, 206) solid 2px;
    padding-top: 10px;
    padding-bottom: 20px;
    margin-left: 32px;
    letter-spacing: 1px;
  }
  .cards{
    width: 820px;
    min-height: 100px;
    border: solid 1px rgb(224, 224, 224);
    background-image: radial-gradient(circle at 20px, transparent 8px,rgba(255, 255, 255,1) 0px);
    /*background-color: rgba(2555,255,255,0.8);*/
    border-radius: 3px;
    margin-top: 10px;
    margin-left: -35px;
    transition: 0.3s;
    position: relative;
  }
  .topping{
    position: absolute;
    top: -33px;
    right: -33px;
    width: 30px;
    height: 30px;
    border: transparent solid 18px;
    border-bottom: rgb(255, 76, 76) solid 18px;
    transform: rotate(45deg);
  }
  .top-text{
    position: absolute;
    top: 10px;
    right: 6px;
    width: 30px;
    height: 30px;
    font-size: 10px;
    color: rgb(243, 243, 243);
    transform: rotate(45deg);
  }
  .cards:hover{
    box-shadow: 0 0 5px rgb(193, 193, 193);
    transform:translateY(-2px);
  }
  .cards-top{
    line-height: 100%;
    padding-left: 45px;
    padding-right: 45px;
    padding-top: 20px;
    padding-bottom: 10px;
    color: rgb(77, 77, 77);
    font-size: 16px;
    line-height: 22px;
  }
  .cards-bottom{
    height: 40px;
  }
  .cards-bottom div{
    float: left;
    color: rgb(126, 126, 126);

  }
  .cards-bottom-headPho{
    width: 550px;
  }
  .cards-bottom-headPho div:nth-child(1){
    width: 20px;
    height: 20px;
    margin-top: 8px;
    margin-left: 45px;
  }
  .cards-bottom-headPho div:nth-child(2){
    height: 20px;
    width: 150px;
    margin-left: 5px;
    margin-top: 8px;
    line-height: 20px;
    font-size: 10px;
    /* 文字溢出处理 */
    display: -webkit-box;
    overflow:hidden;
    text-overflow: ellipsis;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:1;
  }
  .cards-bottom-pubTime{
    width: 200px;
    line-height: 36px;
    font-size: 11px;
    margin-left: -40px;
  }
  .cards-bottom-like{
    width: 80px;
    line-height: 35px;
    font-size: 11px;
  }
  .icon-xihuan{
    color: rgb(124, 124, 124);
    font-size: 20px;
    color: rgba(209,80,57,0.8);
  }
  .icon-xihuan:hover{
    color: rgba(254, 12, 0, 0.8);
  }
  .icon-xihuan:active{
    color: rgba(209,80,57,0.8);
  }
  .cards-bottom-icn{
    margin-right: 5px;
  }
  .page{
    margin: 10px auto;
    margin-left: -10px;
  }
</style>
